//
//  LGMineViewController.m
//  lgdw
//
//  Created by mac on 16/1/22.
//  Copyright © 2016年 Apple. All rights reserved.
//

#import "LGMineViewController.h"
#import "LGMTopicViewController.h"
#import "LGMShotrViewController.h"
#import "LGMAttentionViewController.h"
#import "LGMFansViewController.h"

@interface LGMineViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *iconImage;
@property (weak, nonatomic) IBOutlet UIButton *oneButton;
@property (weak, nonatomic) IBOutlet UIButton *twoButton;
@property (weak, nonatomic) IBOutlet UIButton *threeButton;
@property (weak, nonatomic) IBOutlet UIButton *fourButton;
@property (weak, nonatomic) IBOutlet UIView *contentView;

@property(nonatomic, weak) UIViewController *showingChildVc;

// 按钮点击
- (IBAction)oneButtonClick:(UIButton *)sender;
- (IBAction)twoButton:(UIButton *)sender;
- (IBAction)threeButton:(UIButton *)sender;
- (IBAction)fourButtonClick:(UIButton *)sender;

@property (nonatomic , weak) UIButton *currentBtn;

@property (nonatomic , strong) UIView *bottomView;



@end

@implementation LGMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 添加控制器
    [self addChildViewController:[[LGMShotrViewController alloc] init]];
    [self addChildViewController:[[LGMTopicViewController alloc] init]];
    [self addChildViewController:[[LGMAttentionViewController alloc] init]];
    [self addChildViewController:[[LGMFansViewController alloc] init]];
    
    // 加载左右两边的button
    [self addNavgationButton];
    // 设置图片为圆形
    self.iconImage.layer.cornerRadius = 50;
    self.iconImage.layer.borderWidth = 1;
    self.iconImage.layer.borderColor = [UIColor whiteColor].CGColor;
    self.iconImage.layer.masksToBounds = YES;
    // 设置button按钮
    [self.oneButton setTitle:@"晒物" Nun:-0];
    [self.twoButton setTitle:@"众测" Nun:-0];
    [self.threeButton setTitle:@"关注" Nun:-0];
    [self.fourButton setTitle:@"粉丝" Nun:-0];
    [self oneButtonClick:self.oneButton];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

/** 添加左右两侧的button */
- (void)addNavgationButton {
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"message_button"] style:UIBarButtonItemStylePlain target:self action:@selector(messageButtonClick)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"setting_button"] style:UIBarButtonItemStylePlain target:self action:@selector(settingButtonClick)];
}

// 点击信息
- (void)messageButtonClick {
    NSLog(@"点击了信息");
}

// 点击设置
- (void)settingButtonClick {
    NSLog(@"点击了设置");
}


// 点击晒物按钮
- (IBAction)oneButtonClick:(UIButton *)sender {

    self.currentBtn.selected = NO;
    sender.selected = YES;
    self.currentBtn = self.oneButton;
    
    
    [sender addSubview:self.bottomView];
     [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
         make.top.mas_equalTo(sender.mas_bottom);
         make.width.mas_equalTo(sender.mas_width);
         make.height.mas_equalTo(2);
         make.left.mas_equalTo(sender.mas_left);
     }];
    
    [self switchVc:0];
    
    
}
// 点击众测按钮
- (IBAction)twoButton:(UIButton *)sender {

    self.currentBtn.selected = NO;
    sender.selected = YES;
    self.currentBtn = self.twoButton;
    
    [sender addSubview:self.bottomView];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(sender.mas_bottom);
        make.width.mas_equalTo(sender.mas_width);
        make.height.mas_equalTo(2);
        make.left.mas_equalTo(sender.mas_left);
    }];
    
    [self switchVc:1];

}
// 点击关注按钮
- (IBAction)threeButton:(UIButton *)sender {

    self.currentBtn.selected = NO;
    sender.selected = YES;
    self.currentBtn = self.threeButton;
    
    [sender addSubview:self.bottomView];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(sender.mas_bottom);
        make.width.mas_equalTo(sender.mas_width);
        make.height.mas_equalTo(2);
        make.left.mas_equalTo(sender.mas_left);
    }];
    
    [self switchVc:2];
}
// 点击粉丝按钮
- (IBAction)fourButtonClick:(UIButton *)sender {

    self.currentBtn.selected = NO;
    sender.selected = YES;
    self.currentBtn = self.fourButton;
    
    
    [sender addSubview:self.bottomView];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(sender.mas_bottom);
        make.width.mas_equalTo(sender.mas_width);
        make.height.mas_equalTo(2);
        make.left.mas_equalTo(sender.mas_left);
    }];
    
    [self switchVc:3];
}


- (UIView *)bottomView {
    if (_bottomView == nil) {
        _bottomView = [[UIView alloc] init];
        _bottomView.backgroundColor = [[UIColor alloc] initWithRed:63 / 255.0 green:156 / 255.0 blue:157 / 255.0 alpha:1.0];
        
    }
    return _bottomView;
}



- (void)switchVc:(int)index
{
    
    UIViewController *newVc = self.childViewControllers[index];
    // 如果index对应的子控制器正在显示,就直接返回
//    if (newVc == self.showingChildVc) return;
    
    // 保存新旧控制器的索引
    NSUInteger newIndex = index;
    NSUInteger oldIndex = [self.childViewControllers indexOfObject:self.showingChildVc];
    
    // 保证点击后,让旧索引等于新索引的值
    if (oldIndex == NSNotFound) {
        oldIndex = newIndex;
    }
    
    // 判断并执行动画
    if (newIndex >= oldIndex) { // 从右向左移动
        CGFloat newX = self.contentView.frame.size.width;
        CGFloat oldX = - self.view.frame.size.width;
        [self performAnimationWithNewVc:newVc NewX:newX oldX:oldX];
    } else {  // 从左向右移动
        CGFloat newX = - self.contentView.frame.size.width;
        CGFloat oldX = self.view.frame.size.width;
        [self performAnimationWithNewVc:newVc NewX:newX oldX:oldX];
    }
}

// 执行动画
- (void)performAnimationWithNewVc:(UIViewController *)newVc NewX:(CGFloat)newX oldX:(CGFloat)oldX
{
    // 设置新控制器 view 的 frame
    CGRect newFrame = self.contentView.bounds;
    newFrame.origin.x = newX;
    newVc.view.frame = newFrame;
    [self.contentView addSubview:newVc.view];
    
    // 动画
    [UIView animateWithDuration:0.5 animations:^{
        CGRect oldFrame = self.showingChildVc.view.frame;
        oldFrame.origin.x = oldX;
        self.showingChildVc.view.frame = oldFrame;
        
        newVc.view.frame = self.contentView.bounds;
    }completion:^(BOOL finished) {
        [self.showingChildVc.view removeFromSuperview];
        self.showingChildVc = newVc;
    }];
}


@end
